package tech.runchen.gz5055.oms.common.utils.redis.delay;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.runchen.gz5055.oms.common.enums.OrderStatus;
import tech.runchen.gz5055.oms.common.enums.ReturnOrderStatus;
import tech.runchen.gz5055.oms.common.utils.SpringUtils;
import tech.runchen.gz5055.oms.modules.system.entity.OrderEntity;
import tech.runchen.gz5055.oms.modules.system.entity.ReturnOrderEntity;
import tech.runchen.gz5055.oms.modules.system.service.INoticeService;
import tech.runchen.gz5055.oms.modules.system.service.IOrderService;
import tech.runchen.gz5055.oms.modules.system.service.IReturnOrderService;

/**
 * @author zzj
 */
public class ReturnTimeoutCheckTask extends AbstractRedisDelayTask {

    private final static Logger logger = LoggerFactory.getLogger(ReturnTimeoutCheckTask.class);
    public final static String TAG = "RETURN_ORDER_ID:";
    public final static long TIMEOUT = 5 * 60;

    public ReturnTimeoutCheckTask(String id, String value, long delayTime) {
        super(id, value, delayTime);
    }

    @Override
    public void execute() {
        logger.info("task execute, {}", this);
        long returnOrderId = Long.valueOf(this.getValue());
        // 获取Bean
        IReturnOrderService returnOrderService = SpringUtils.getBean(IReturnOrderService.class);
        INoticeService noticeService = SpringUtils.getBean(INoticeService.class);
        ReturnOrderEntity returnOrder = returnOrderService.getById(returnOrderId);
        if (returnOrder != null) {
            if (returnOrder.getStatus() == ReturnOrderStatus.WAIT.getValue() || returnOrder.getStatus() == ReturnOrderStatus.CONFIRM.getValue()) {
                RedisDelayQueueManager redisDelayQueueManager = SpringUtils.getBean(RedisDelayQueueManager.class);
                // 创建即时通知
                noticeService.createReturnOrderNotice(returnOrderService.getById(returnOrderId), ReturnOrderStatus.TIMEOUT_CHECKED);
                // 设置下一次提醒时间
                redisDelayQueueManager.addTask(new ReturnTimeoutCheckTask(this.getId(), this.value, TIMEOUT));
            }
        }
    }
}
